Fix FD leak in connSocketBlockingConnect on timeout#3541
Merged
enjoy-binbin merged 1 commit intovalkey-io:unstablefrom Apr 23, 2026
Merged
Fix FD leak in connSocketBlockingConnect on timeout#3541enjoy-binbin merged 1 commit intovalkey-io:unstablefrom
enjoy-binbin merged 1 commit intovalkey-io:unstablefrom
Conversation
25abc3b to
0e444b8
Compare
Assign conn->fd immediately after anetTcpNonBlockConnect() succeeds, so that the caller's connClose() properly cleans up the fd on any subsequent error (e.g., aeWait timeout). Previously, conn->fd was only assigned on success, so a timeout left the fd orphaned — one leak per failed MIGRATE connection attempt. Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
0e444b8 to
7474835
Compare
xdk-amz
approved these changes
Apr 20, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #3541 +/- ##
============================================
+ Coverage 76.41% 76.44% +0.02%
============================================
Files 159 159
Lines 79927 79927
============================================
+ Hits 61078 61097 +19
+ Misses 18849 18830 -19
🚀 New features to boost your workflow:
|
enjoy-binbin
approved these changes
Apr 22, 2026
Member
|
I am merging it and added the backport label, let me know if you think otherwise. |
sarthakaggarwal97
pushed a commit
to sarthakaggarwal97/valkey
that referenced
this pull request
Apr 23, 2026
## Summary Fix a file descriptor leak in `connSocketBlockingConnect()` when `aeWait()` times out. ## Bug When `anetTcpNonBlockConnect()` succeeds but `aeWait()` times out (e.g., MIGRATE to an unreachable host), the fd is leaked because it was never assigned to `conn->fd`. The caller's `connClose()` checks `conn->fd != -1` and skips cleanup. ## Fix Assign `conn->fd = fd` immediately after `anetTcpNonBlockConnect()` succeeds, before `aeWait()`. This way the caller's normal `connClose()` cleanup path handles the fd on any error, which is consistent with how the rest of the connection lifecycle works. TLS connections also benefit since `connTLSBlockingConnect` delegates to this function for the TCP layer. ## Reproducer ``` valkey-cli SET key hello # Repeat against unreachable host: for i in $(seq 1 30); do valkey-cli MIGRATE 192.0.2.1 6379 key 0 500; done # Check: /proc/<pid>/fd shows 30 leaked socket fds ``` *This issue was generated by AI but verified, with love, by a human.* Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix a file descriptor leak in
connSocketBlockingConnect()whenaeWait()times out.Bug
When
anetTcpNonBlockConnect()succeeds butaeWait()times out (e.g., MIGRATE to an unreachable host), the fd is leaked because it was never assigned toconn->fd. The caller'sconnClose()checksconn->fd != -1and skips cleanup.Fix
Assign
conn->fd = fdimmediately afteranetTcpNonBlockConnect()succeeds, beforeaeWait(). This way the caller's normalconnClose()cleanup path handles the fd on any error, which is consistent with how the rest of the connection lifecycle works.TLS connections also benefit since
connTLSBlockingConnectdelegates to this function for the TCP layer.Reproducer
This issue was generated by AI but verified, with love, by a human.